package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowow.comment.domain.StrategyComment;
import cn.wolfcode.luowow.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowow.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyThemeService strategyThemeService;

    @Reference
    private IStrategySearchService strategySearchService;


    @RequestMapping("")
    public String index(Model model){



        List<StrategyStatisVO> vos = strategyStatisVOService.listComemendVO();

        //进行拆分
        //海外  abroadCds
        List<StrategyStatisVO> abroadCds = new ArrayList<>();

        //国内  unabroadCds
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();

        //要是是国内就加入国内的集合,不是就加入另外一个
        for (StrategyStatisVO vo : vos){
            //我们不需要那么多,只要十个,所有还是需要判断的,要是够了就不要加了
            if (vo.isIsabroad()){
                //国外
                if (abroadCds.size()<10){
                    abroadCds.add(vo);
                }
            }else {
                //国内
                if (unabroadCds.size()<10){
                    unabroadCds.add(vo);
                }
            }
        }

        //海外  abroadCds
        model.addAttribute("abroadCds",abroadCds);
        //国内  unabroadCds
        model.addAttribute("unabroadCds",unabroadCds);
        //热门攻略  hotCds
        model.addAttribute("hotCds",strategyStatisVOService.listHotVOTop10());

        //主题  themeCds
        //按关联多少目的地数量排序
        //去除重复的目的地
        //只需要前十个
        List<Map<String,Object>> themeCds = strategySearchService.queryThemeTop10();
        model.addAttribute("themeCds",themeCds);

        //themes
        List<StatisVo> themes = strategySearchService.queryConditionThemeGroup();
        model.addAttribute("themes",themes);

        //国外 abroads
        List<StatisVo> abroads = strategySearchService.queryConditionAbroadsGroup();
        model.addAttribute("abroads",abroads);

        //国内 chinas
        List<StatisVo> chinas = strategySearchService.queryConditionProvincesGroup();
        model.addAttribute("chinas",chinas);

        //攻略推荐  commends
        model.addAttribute("commends",strategyCommendService.getCommendTop5());

        return "strategy/index";
    }

    @RequestMapping("detail")
    public String index(Model model,Long id){

        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail",detail);

        //阅读数＋1
        strategyStatisVOService.viewnumIncrease(id,1);
        model.addAttribute("vo",strategyStatisVOService.getStrategyStatisVo(id));


        //热门攻略加分
        strategyStatisVOService.addHotScore(id,1);


        return "strategy/detail";
    }

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){

        //toasts  dest
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //tags
        model.addAttribute("tags",strategyTagService.list());

        //pageInfo
        model.addAttribute("pageInfo",strategyDetailService.query(qo));

        return "strategy/list";
    }
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        if (userInfo == null){
            throw new LogicException("检查是否已经登录");
        }

        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUserId(userInfo.getId());
        comment.setLevel(userInfo.getLevel());
        comment.setCity(userInfo.getCity());
        comment.setUsername(userInfo.getNickname());

        strategyCommentService.save(comment);

        //评论数＋1
        strategyStatisVOService.replynumIncrease(comment.getDetailId(),1);

        int replynum = strategyStatisVOService.getStrategyStatisVo(comment.getDetailId()).getReplynum();

        //热门攻略加分
        strategyStatisVOService.addHotScore(comment.getDetailId(),1);


        return AjaxResult.SUCCESS.addData(replynum);
    }


    @RequestMapping("/comment")
    public String comment(Model model,@ModelAttribute("qo") StrategyCommentQuery qo){

        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);

        return "strategy/commentTpl";
    }

    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){//toid:文档id fromid:用户id
        strategyCommentService.commentThumbUp(toid,fromid);


        return AjaxResult.SUCCESS;

    }

    //攻略收藏
    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){

        if (userInfo == null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.RESPONSE_CODE_NOLOGIN);
            return result;
        }

        boolean ret = strategyStatisVOService.favor(sid,userInfo.getId());

        strategyStatisVOService.favor(sid,userInfo.getId());
        return new AjaxResult(ret,null).addData(strategyStatisVOService.getStrategyStatisVo(sid));
    }


    //攻略顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo) {

        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.RESPONSE_CODE_NOLOGIN);
            return result;
        }
        //true:收藏 false:取消收藏
        boolean ret = strategyStatisVOService.thumbup(sid,userInfo.getId());

        //加分或减分
        strategyStatisVOService.addCommendScore(sid,ret?1:-1);

        //true:收藏 false:取消收藏
        return new AjaxResult(ret,null).addData(strategyStatisVOService.getStrategyStatisVo(sid));
    }


    @RequestMapping("/searchPage")
    public String searchPage(Model model,@ModelAttribute("qo") StrategySearchQuery qo){

        //分页查询
        Page page = strategySearchService.query(qo);
        model.addAttribute("page",page);

        return "strategy/searchPageTpl";

    }
}
